package com.winit.tag.repository;

import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.winit.tag.model.ConfigTag;
import com.winit.tag.model.ConfigTagGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * Created by wangjian on 2017/9/28.
 */
@Component
public class TagOperations {
    private static Logger logger = LoggerFactory.getLogger(TagOperations.class);

    @Autowired
    private CassandraOperations cassandraOperations;

    public List<ConfigTag> getTag(String groupCode, String code) {
        Select select = QueryBuilder.select().from("uconfig_tag");
        if (!StringUtils.isEmpty(groupCode)) {
            select.where(QueryBuilder.eq("tag_group_code", groupCode));
        }
        if (!StringUtils.isEmpty(code)) {
            select.where(QueryBuilder.eq("tag_code", code));
        }
        List<ConfigTag> list = cassandraOperations.select(select, ConfigTag.class);
        return list;
    }

    public List<ConfigTagGroup> getTagGroup(String groupCode) {
        Select select = QueryBuilder.select().from("uconfig_tag_group");
        if (!StringUtils.isEmpty(groupCode)) {
            select.where(QueryBuilder.eq("group_code", groupCode));
        }
        List<ConfigTagGroup> list = cassandraOperations.select(select, ConfigTagGroup.class);
        return list;
    }

}
